//
//  NZKNoDataView.m
//  ProjectBoss
//
//  Created by 聂志康 on 2018/8/24.
//  Copyright © 2018 weipainetwork. All rights reserved.
//

#import "NZKNoDataView.h"

@interface NZKNoDataView ()
@property (nonatomic, strong) UIImageView *noDataImageView;
@property (nonatomic, strong) UILabel *noDataTipLabel;
@property (nonatomic, strong) UIButton *noDataButton;
@end

@implementation NZKNoDataView

- (instancetype)initWithFrame:(CGRect)frame withTip:(NSString * _Nullable)tip andButtonText:(NSString * _Nullable)text {
    self = [super initWithFrame:frame];
    if (self) {
        _tip = tip;
        _text = text;
        self.backgroundColor = hexColor(ffffff);
        [self addSubview:self.noDataImageView];
        [self addSubview:self.noDataTipLabel];
        if (_tip) {
            self.noDataTipLabel.text = _tip;
        }
        if (_text) {
            [self addSubview:self.noDataButton];
            [self.noDataButton setTitle:_text forState:UIControlStateNormal];
        }
    }
    return self;
}

- (void)setImage:(NSString *)image {
    _image = image;
    self.noDataImageView.image = [UIImage imageNamed:_image];
    [self setNeedsLayout];
}

- (void)setTip:(NSString *)tip {
    _tip = tip;
    self.noDataTipLabel.text = _tip;
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text {
    _text = text;
    [self addSubview:self.noDataButton];
    [self.noDataButton setTitle:_text forState:UIControlStateNormal];
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    WS
    UIImage *image = self.noDataImageView.image;
    CGSize imageSize = CGSizeZero;
    if (image) {
        imageSize = image.size;
    }
    [self.noDataImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.equalTo(weakSelf.mas_top).with.offset(kHeight(56));
        make.size.mas_equalTo(imageSize);
    }];
    [self.noDataTipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).with.offset(12);
        make.right.equalTo(weakSelf.mas_right).with.offset(-12);
        make.top.equalTo(weakSelf.noDataImageView.mas_bottom).with.offset(40);
    }];
    if (_text) {
        CGRect TitleRect = [_text boundingRectWithSize:CGSizeMake(ScreenWidth, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12]} context:nil];
        [self.noDataButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.noDataTipLabel.mas_bottom).with.offset(25);
            make.centerX.equalTo(weakSelf.noDataTipLabel.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(TitleRect.size.width+25, TitleRect.size.height+12));
        }];
    }
}

- (UIImageView *)noDataImageView {
    if (!_noDataImageView) {
        _noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noDataImage"]];
    }
    return _noDataImageView;
}

- (UILabel *)noDataTipLabel {
    if (!_noDataTipLabel) {
        _noDataTipLabel = [[UILabel alloc] init];
        _noDataTipLabel.textColor = hexColor(999999);
        _noDataTipLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
        _noDataTipLabel.textAlignment = NSTextAlignmentCenter;
        _noDataTipLabel.numberOfLines = 0;
    }
    return _noDataTipLabel;
}

- (UIButton *)noDataButton {
    if (!_noDataButton) {
        _noDataButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-40, 245, 80, 24)];
        [_noDataButton setTitleColor:NAV_COLOR forState:UIControlStateNormal];
        _noDataButton.layer.cornerRadius = 5;
        _noDataButton.layer.masksToBounds = true;
        _noDataButton.layer.borderColor = NAV_COLOR.CGColor;
        _noDataButton.layer.borderWidth = 0.5;
        _noDataButton.titleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:12];
        [_noDataButton addTarget:self action:@selector(noDataButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noDataButton;
}

- (void)noDataButtonClick {
    if ([self.delegate respondsToSelector:@selector(noDataViewButtonClick)]) {
        [self.delegate noDataViewButtonClick];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
